package com.cabinet.ui;

import com.cabinet.Config;
import com.cabinet.Device;
import com.cabinet.db.DBDeviceState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* Created with IntelliJ IDEA.
* User: lesya
* Date: 12/9/13
* Time: 9:14 AM
* To change this template use File | Settings | File Templates.
*/
public abstract class DevicePanel extends JPanel {
  abstract void devicesConfigChanged();

  final static int TOTAL_H = 71;
  final static int TOTAL_W = 160;
  final static int HEAD_W = 120;

  long conferenceId;

  long devId;
  JPopupMenu popupMenu = new JPopupMenu();
  boolean groupSelected=false;
  JLabel imgBg;
  JLabel imgHead = new JLabel();
  JLabel imgBigNum = new JLabel();
  JButton btnSelf = new JButton();
  JButton btnMike = new JButton();
  JButton btnHeadset = new JButton();

  JCheckBoxMenuItem speak = new JCheckBoxMenuItem("Говорить");
  JCheckBoxMenuItem listen = new JCheckBoxMenuItem("Слухає");
  JCheckBoxMenuItem selftest = new JCheckBoxMenuItem("Selftest");

  volatile int draggedAtX, draggedAtY;

  void selfTestSet(boolean val){
    if( !Device.hasSelfTest(devId) ){
      val = false;
    }
    selftest.setSelected(val);
    //if( true ){
      conferenceId=-1;
    //}
    if(val){
      listen.setSelected(false);
      speak.setSelected(false);
    }
    switchChange(selftest);
  }

  // for new TransferHandler("devId")
  public long getDevId() {
    return devId;
  }

  public DevicePanel(final long devId) {
    this.devId=devId;

    setOpaque(false);
    setLayout(null);

    selftest.setVisible(Device.hasSelfTest(devId));

    if( devId <= Device.LAST_STUD ){
      add(btnSelf);
      ImageIcon iconSelf = new ImageIcon(Config.getScaledImage("knp/Knp_self.png", 0, 20));
      btnSelf.setSize(iconSelf.getIconWidth(),iconSelf.getIconHeight());
      btnSelf.setIcon(iconSelf);
      btnSelf.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
          selfTestSet( !selftest.isSelected() );
        }
      });
    }
    boolean audioDev = Device.isAudioDevice(devId);
    ImageIcon iconMike = new ImageIcon(Config.getScaledImage(audioDev ? "knp/Knp_pl.png" : "knp/Knp_mk.png", 0, audioDev ? 30 : 20));
    btnMike.setSize(iconMike.getIconWidth(),iconMike.getIconHeight());
    btnMike.setIcon(iconMike);
    add(btnMike);
    btnMike.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent actionEvent) {
        speak.setSelected( !speak.isSelected() );
        switchChange(speak);
     }
    });
    ImageIcon iconHeadset = new ImageIcon(Config.getScaledImage(audioDev ? "knp/Knp_re.png" : "knp/Knp_tl.png", 0, audioDev ? 20 : 30));
    btnHeadset.setSize(iconHeadset.getIconWidth(),iconHeadset.getIconHeight());
    btnHeadset.setIcon(iconHeadset);
    add(btnHeadset);
    btnHeadset.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent actionEvent) {
        listen.setSelected( !listen.isSelected() );
        switchChange(listen);
      }
    });

    add(imgBigNum);
    add(imgHead);
    imgBg = new JLabel();
    imgBg.setBounds(0,0, TOTAL_W, TOTAL_H);
    add(imgBg);
    imgBg.setInheritsPopupMenu(true);

    String bigNum;
    if( devId==Device.TEACHER ){
      bigNum = "";
    } else if( devId==Device.DEVICE1 ){
      bigNum = "A01.png";
    } else if( devId==Device.DEVICE2 ){
      bigNum = "A02.png";
    } else if( devId==Device.DEVICE3 ){
      bigNum = "A03.png";
    } else if( devId==Device.DEVICE4 ){
      bigNum = "A04.png";
    } else {
      String s = ""+(devId+1);
      if( s.length()==1 ){
        s = "0" + s;
      }
      bigNum = "Y"+s+".png";
    }
    if( bigNum.equals("") ){
      imgBigNum.setSize(0,0);
    }else{
      ImageIcon iconBigNum = new ImageIcon(Config.getScaledImage("bignum/"+bigNum, 0, 25));
      imgBigNum.setIcon(iconBigNum);
      imgBigNum.setSize(iconBigNum.getIconWidth(), iconBigNum.getIconHeight());
    }


    popupMenu.add(speak);
    popupMenu.add(listen);
    popupMenu.add(selftest);
    //setComponentPopupMenu(popupMenu);

    ActionListener menuCheckbox = new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        switchChange((JCheckBoxMenuItem) e.getSource());
      }
    };
    speak.addActionListener(menuCheckbox);
    listen.addActionListener(menuCheckbox);
    selftest.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent actionEvent) {
        selfTestSet(selftest.isSelected());
      }
    });


  }

  void toggleGroupSelected(){
    setGroupSelected( ! groupSelected );
  }

  public void setGroupSelected(boolean on){
    if( on != groupSelected ){
      groupSelected = on;
      setImageBgIcon();
    }
  }

  void switchChange(JCheckBoxMenuItem sw){
    if( Device.isAudioDevice(devId) ){
      if( speak.isSelected() && listen.isSelected() ){
        if( sw==speak ){
          listen.setSelected(false);
        }
        if( sw==listen ){
          speak.setSelected(false);
        }
      }
    }
    if( selftest.isSelected() && listen.isSelected() && sw==listen ){
      selftest.setSelected(false);
      conferenceId = DBDeviceState.TEACHERS_CONFERENCE;
    }
    devicesConfigChanged();
  }

  void setDeviceState(DBDeviceState state){
    conferenceId = state.getConferenceId();
    speak.setSelected(state.isSpeak());
    listen.setSelected(state.isListen());
    selftest.setSelected(state.isSelfTest());

    setImageBgIcon();

    ImageIcon iconHead = new ImageIcon(Config.getScaledImage("head/"+state.headImage(), HEAD_W, 0));
    imgHead.setIcon(iconHead);
    imgHead.setBounds((TOTAL_W-HEAD_W)/2, 10, iconHead.getIconWidth(), iconHead.getIconHeight());
    imgBigNum.setLocation(imgHead.getX()+4, imgHead.getY()+4);
    btnSelf.setLocation(imgHead.getX()+4, imgHead.getY()+35);
    if( Device.isAudioDevice(state.getDevId()) ){
      btnHeadset.setLocation(imgHead.getX()+imgHead.getWidth()-35, imgHead.getY()+35);
      btnMike.setLocation(imgHead.getX()+imgHead.getWidth()-35, imgHead.getY()+4);
    }else{
      btnHeadset.setLocation(imgHead.getX()+imgHead.getWidth()-35, imgHead.getY()+4);
      btnMike.setLocation(imgHead.getX()+imgHead.getWidth()-35, imgHead.getY()+35);
    }
    btnHeadset.setEnabled(state.canSwitchListen());
    listen.setEnabled(state.canSwitchListen());

  }

  private void setImageBgIcon() {
    String img = "of.png";
    if( selftest.isSelected() ){
      img = "st.png";
    } else if( conferenceId>=0 && conferenceId<=7 ){
      img = "0" + (conferenceId + 1) + ".png";
    }else if( conferenceId==8 ){
      img = "vc.png";
    }
    String path = "R/R" + img;
    if( groupSelected ){
      path = "RS/RS" + img;
    }
    imgBg.setIcon( new ImageIcon(Config.getScaledImage(path, TOTAL_W, TOTAL_H)) );
  }
}
